<?php namespace Summer\Hjd\Controllers;

use Backend\Classes\Controller;
use BackendMenu;
use Db;
use Config;
class RuleConfig extends Controller
{
    public $requiredPermissions = [
        'hjd_bonus_config'
    ];

    public function __construct()
    {
        parent::__construct();
        \BackendMenu::setContext('Winter.System', 'main-menu-item-hjd_enduser');
        \Summer\Hub\Classes\SideNavManager::setContext('Summer.Hjd', 'side-menu-item-hjd_bonus_config');

        new \Summer\Hjd\Widgets\RuleBonus($this,'rulebonus');
        new \Summer\Hjd\Widgets\RuleRg($this,'rulerg');
        new \Summer\Hjd\Widgets\RuleWd($this,'rulewd');
        new \Summer\Hjd\Widgets\RuleTf($this,'ruletf');
    }

    public function index()
    {
        //星期
        $weeks=Config::get('summer.hjd::weeks');
        $week_arr=[];
        foreach ($weeks as $wk=>$wv){
            $week['value']=$wk;
            $week['text']=$wv;
            $week_arr[]=$week;
        }
        $this->vars['week_json'] = json_encode($week_arr);
        //日期
        $day_arr=[];
        for($i=1;$i<=31;$i++){
            $day['value']=$i;
            $day['text']=$i.'日';
            $day_arr[]=$day;
        }
        $this->vars['day_json'] = json_encode($day_arr);
        $this->pageTitle="规则设置";
        $this->addCss('/plugins/summer/hub/assets/bootstrap337/css/bootstrap-foreditable.css');
        $this->addJs('/plugins/summer/hub/assets/bootstrap337/js/bootstrap.js');
        $this->addCss('/plugins/summer/hub/assets/bootstrap3-editable/css/bootstrap-editable.css');
        $this->addJs('/plugins/summer/hub/assets/bootstrap3-editable/js/bootstrap-editable.min.js');
    }

    /**
     * 快速编辑奖金设置
     * @return array
     */
    public function onQuickedit()
    {
        $return['status']=true;//状态
        $return['msg']="操作成功";//信息
        $return['data']="";//数据
        $pdata=request()->all();

        //模型
        $data_mod=$pdata['table'];
        if(empty($data_mod)){
            $data_mod="summer_hjd_bonus_rule";
        }
        $pkname='id';//应该在这里获取主健字段的，暂时没找到方法
        //id
        $data_id=$pdata['pk'];
        //字段
        $data_zd=$pdata['name'];
        $val=$pdata['value']??'';
        //表单类型
        $type=$pdata['type'];
        //开关的特殊操作
        if($type=="switch"){
            if($val=="true"){
                $val=1;
            }else{
                $val=0;
            }
        }
        //复选框,需要字段支持$jsonable。需要处理
        if($type=="checklist"){
            $val=json_encode($val);
        }
        $data[$data_zd]=$val;
        $res=Db::table($data_mod)->where($pkname,$data_id)->update($data);
        if($res){
            return $return;
        }else{
            $return['status']=false;//状态
            $return['msg']="操作失败";//信息
            return $return;
        }
    }
}
